package com.eking.services;

import java.util.List;
import java.util.Optional;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceGroupMemberusersListRequest;
import com.dingtalk.api.response.OapiAttendanceGroupMemberusersListResponse;
import com.eking.tasks.ScheduleTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * 考勤人员
 * Created by hexp on 2023/9/6 20:11
 */
@Component
@Slf4j
public class UsersApi {

    @Autowired
    private GroupsApi groupsApi;
    @Autowired
    private ScheduleTask scheduleTask;

    /**
     * 根据考勤组id 获取人员id
     */
    public Optional<List<String>> getMemberusersList(){
        Optional<Long> groupId = groupsApi.getAttendanceGroup();
        groupId.orElseThrow(() -> new RuntimeException("groupId 获取失败"));
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/memberusers/list");
        OapiAttendanceGroupMemberusersListRequest req = new OapiAttendanceGroupMemberusersListRequest();
        req.setCursor(0L);
        // req.setOpUserId("user123");
        req.setGroupId(groupId.get());
        OapiAttendanceGroupMemberusersListResponse rsp = null;
        try {
            rsp = client.execute(req, scheduleTask.getToken());
        } catch (Exception e) {
            log.error("请求获取人员id接口失败",e);
            throw new RuntimeException(e);
        }
        Assert.isTrue(rsp.isSuccess(),"获取人员id失败");
        return Optional.of(rsp).map(OapiAttendanceGroupMemberusersListResponse::getResult)
                .map(OapiAttendanceGroupMemberusersListResponse.PageResult::getResult);
    }


}
